*******set the file of database****************************
cd E:\data\cooperation\government\final_data


********add the labels********
use final_3rd,clear

label var pat_uspt "patent grants"
label var wdi_trade "The sum of exports and imports of goods and services (% of GDP)"
label var lpop_dnst "Log of Population density (people per sq. km of land area)"
label var wbgi_pse "Political Stability"
label var fh_ipolity2 "Level of Democracy (Freedom House/Imputed Polity)"
label var hf_efiscore "Economic Freedom Index"
label var lgdppc "Log of GDP per capita, PPP (constant 2005 international $)"
label var vdem_corr "Political corruption"
label var fe_cultdiv "Cultural Diversity"
label var wdi_telephone "Telephone lines (per 100 people)"
label var ciri_polpris "Political Imprisonment"
label var taxburden "Tax burden"
label var wdi_internetserv "Secure Internet servers (per 1 million people)"
label var rq "The quality of govenment regulation"
label var wdi_internetuse "Internet users (per 100 people)"
label variable wbgi_gee "Government effectiveness"
label variable governmentintegrity "Government integrity"
label variable regioncode "The code of region"
save final_3rd,replace
des

use original,clear 
label var pat_uspt "patent grants"
label var wdi_trade "The sum of exports and imports of goods and services (% of GDP)"
label var lpop_dnst "Log of Population density (people per sq. km of land area)"
label var wbgi_pse "Political Stability"
label var fh_ipolity2 "Level of Democracy (Freedom House/Imputed Polity)"
label var hf_efiscore "Economic Freedom Index"
label var lgdppc "Log of GDP per capita, PPP (constant 2005 international $)"
label var vdem_corr "Political corruption"
label var fe_cultdiv "Cultural Diversity"
label var wdi_telephone "Telephone lines (per 100 people)"
label var ciri_polpris "Political Imprisonment"
label var taxburden "Tax burden"
label var wdi_internetserv "Secure Internet servers (per 1 million people)"
label var rq "The quality of government regulation"
label var wdi_internetuse "Internet users (per 100 people)"
save original,replace
des 

use original1,clear
egen id = group(countrycode)
rename pat_uspt lpat_uspt
rename wdi_internetserv l_wdi_internetserv
xtset id year 

collin lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse  fh_ipolity2 hf_efiscore ///
vdem_corr fe_cultdiv wdi_telephone taxburden ciri_polpris l_wdi_internetserv ///
urbaniz lrd_pc fincialfreedom monetaryfreedom fh_fotpsc kun_wiqreco_all rsf_pfi ///
al_language al_ethnic laborfreedom p_durable fh_rol une_pee wdi_internetuse governmentintegrity /*table A2*/ 
 
 sum lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse  fh_ipolity2 hf_efiscore ///
vdem_corr fe_cultdiv wdi_telephone taxburden ciri_polpris l_wdi_internetserv ///
urbaniz lrd_pc fincialfreedom monetaryfreedom fh_fotpsc kun_wiqreco_all rsf_pfi ///
al_language al_ethnic laborfreedom p_durable fh_rol une_pee wdi_internetuse governmentintegrity
****************************the main analysis*******************
/*load and deal with the data*/
use final_3rd,clear
egen id = group(countrycode)
gen lpat_uspt=ln(pat_uspt)
gen region1=1
replace region1=2 if regioncode==1|regioncode==3|regioncode==5
replace region1=3 if regioncode==4|regioncode==6|regioncode==7
gen lpat_2=lpat_uspt^2
rename wdi_internetserv l_wdi_internetserv
xtset id year 
/*1.statisitcal description*/
sum wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse  fh_ipolity2 hf_efiscore ///
 vdem_corr fe_cultdiv wdi_telephone ciri_polpris taxburden l_wdi_internetserv rq  wdi_internetuse /*table 2*/

/*fisher unitroot test*/
 xtunitroot fisher wbgi_gee, dfuller lags(3) drift
/*collin test*/
collin lpat_uspt wdi_trade lgdppc  lpop_dnst wbgi_pse  fh_ipolity2 hf_efiscore ///
vdem_corr fe_cultdiv wdi_telephone ciri_polpris taxburden l_wdi_internetserv  /*table 3*/
	
****simple regression***
reg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2 hf_efiscore ///
vdem_corr fe_cultdiv wdi_telephone taxburden ciri_polpris l_wdi_internetserv,robust
 est store reg1
 *****add the square of lpat_uspt****
 reg wbgi_gee lpat_uspt lpat_2 wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2 hf_efiscore ///
vdem_corr fe_cultdiv wdi_telephone taxburden ciri_polpris l_wdi_internetserv,robust
 est store reg4
 *****flgs****
 xtgls wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2 hf_efiscore ///
vdem_corr fe_cultdiv wdi_telephone taxburden ciri_polpris l_wdi_internetserv i.year , panels(hetero)
est store reg2
reg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2 hf_efiscore ///
vdem_corr fe_cultdiv wdi_telephone taxburden ciri_polpris l_wdi_internetserv i.regioncode
 est store reg3
esttab  reg1 reg4 reg2 reg3, cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(N r2, fmt(0 3) label(N R-squared ))  /*table 4*/

**********fe model***********************
xtreg wbgi_gee lpat_uspt,fe
est store fe3
xtreg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2 hf_efiscore vdem_corr, fe
est store fe
xtreg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2 hf_efiscore vdem_corr, re
est store re
xtreg wbgi_gee L.lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2 hf_efiscore vdem_corr, fe
est store fe1
xtreg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2 hf_efiscore vdem_corr i.year, fe
est store fe2
esttab fe3 fe re fe1 fe2, cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(N r2, fmt(0 3) label(N R-squared ))  /*table 5*/
  
hausman fe re,sigmamore




  
*********democracy analysis*****
use final_3rd_demo,clear
egen id = group(countrycode)
gen lpat_uspt=ln(pat_uspt)
rename wdi_internetserv l_wdi_internetserv
xtset id year
preserve
**high level
keep if group==1
xtreg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse  hf_efiscore vdem_corr,fe
est store high_demo
esttab  high_demo , cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(N r2_a, fmt(1 3) label(Observations Adj-R-Square))
restore
preserve
******low level
keep if group==0
xtreg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse  hf_efiscore vdem_corr,fe
est store low_demo
esttab  low_demo , cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(N r2_a, fmt(1 3) label(Observations Adj-R-Square))
restore
gen group_lpat_uspt=group*lpat_uspt
xtreg wbgi_gee  group_lpat_uspt  wdi_trade lgdppc lpop_dnst wbgi_pse hf_efiscore vdem_corr i.year,fe
est store all1_demo
esttab all1_demo, cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(N r2_a, fmt(1 3) label(Observations Adj-R-Square))

***************robust test*************
**add more variabls
use final_3rd,clear
egen id = group(countrycode)
gen lpat_uspt=ln(pat_uspt)
gen l_wdi_internetserv=ln(wdi_internetserv)
xtset id year 
xtreg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2  hf_efiscore vdem_corr i.year, fe
est store fe
xtreg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2  hf_efiscore vdem_corr wdi_telephone ciri_polpris i.year, fe
est store fe1
esttab  fe fe1, cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(N r2_a, fmt(1 3) label(Observations Adj-R-Square))
****endogenous analysisi******************
xtivreg wbgi_gee (lpat_uspt=rq  wdi_internetuse ) wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2  hf_efiscore vdem_corr , fe first
est store iv
xtoverid
dmexogxt
esttab   iv, cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(N r2, fmt(0 3) label(Observations R-Square))

*******original data
use original,clear
egen id = group(countrycode)
rename pat_uspt lpat_uspt
rename wdi_internetserv l_wdi_internetserv
xtset id year 


xtreg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2  hf_efiscore vdem_corr i.year, fe
est store fe
esttab  fe , cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(N r2_a, fmt(1 3) label(Observations Adj-R-Square))



 ********GMM model*****
tab year,gen(dum_year)
xtabond2 wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2 hf_efiscore vdem_corr, /// 
         gmm(lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2 hf_efiscore vdem_corr) iv(wdi_telephone ciri_polpris , mz) robust twostep small h(2)
est store gmm

esttab   gmm, cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(N r2, fmt(0 3) label(Observations R-Square))




